package test.my.com.myapplicationtest.CoordinatorLayout_Test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import test.my.com.myapplicationtest.R;
import test.my.com.myapplicationtest.utils.ToastUtil;

/**
 * Author : Cooper
 * Time : 2017/1/7  14:41
 * Description : CoordinatorLayout 测试
 */

public class CoordinatorLayoutActivity extends AppCompatActivity {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rv)
    RecyclerView rv;

    private List<String> stringList = new ArrayList<>();
    private String[] names = {"liming", "xiaoli", "dasha", "xiaohong", "xiaoli", "dasha", "xiaohong", "xiaoli", "dasha", "xiaohong", "dasha", "xiaohong", "xiaoli", "dasha", "xiaohong", "xiaoli", "dasha", "xiaohong", "dasha", "xiaohong", "xiaoli", "dasha", "xiaohong", "xiaoli", "dasha", "xiaohong"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.aty_coordinatorlayout_layout);
        ButterKnife.bind(this);

        initToolBar();
        initRV();
    }

    private void initRV() {
        for (String s : names) {
            stringList.add(s);
        }
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new RvAdapter(stringList));
    }

    private void initToolBar() {

        toolbar.setTitle("仅供测试");
        setSupportActionBar(toolbar);

//        toolbar.inflateMenu(R.menu.tool_bar_menu);
//        toolbar.setLogo(R.drawable.ic_account_leo);
        toolbar.setNavigationIcon(R.drawable.ic_navbar_back_h);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.search_item:
                        ToastUtil.showToast("search_item  == " + item.getTitle().toString());
                        break;
                    case R.id.set_item:
                        ToastUtil.showToast("set_item  == " + item.getTitle().toString());
                        break;
                    case R.id.news_item:
                        ToastUtil.showToast("news_item  == " + item.getTitle().toString());
                        break;
                    case R.id.about_item:
                        ToastUtil.showToast("about_item  == " + item.getTitle().toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.tool_bar_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    class RvAdapter extends RecyclerView.Adapter<RvAdapter.MyViewHolder>{

        private List<String> list;

        public RvAdapter(List<String> list) {
            this.list = list;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(CoordinatorLayoutActivity.this).inflate(R.layout.item_person_layout, null);
            MyViewHolder viewHolder = new MyViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.mName_tv.setText(list.get(position));
            holder.mSex_tv.setText(list.get(position));
            holder.mAge_tv.setText(list.get(position));
            holder.ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ToastUtil.showToast("sdfsadf");
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            private TextView mName_tv;
            private TextView mSex_tv;
            private TextView mAge_tv;
            private LinearLayout ll;
            public MyViewHolder(View itemView) {
                super(itemView);
                ll = (LinearLayout) itemView.findViewById(R.id.ll);
                mName_tv = (TextView) itemView.findViewById(R.id.mName_tv);
                mSex_tv = (TextView) itemView.findViewById(R.id.mSex_tv);
                mAge_tv = (TextView) itemView.findViewById(R.id.mAge_tv);
            }
        }

    }
}
